package com.guinea.generator;

import java.util.Iterator;
import java.util.List;

import com.guinea.core.utils.StringUtils;

public class MappingManyToOne {
	String name;

	String className;

//	String refProperty;

	String column;

	MappingClass parent;

	public MappingManyToOne(String name, String className,
			String column) {
		super();
		this.name = StringUtils.uncapitalize(name);
		this.className = StringUtils.capitalize(className);
//		this.refProperty = StringUtils.uncapitalize(refProperty);
		this.column = StringUtils.defaultString(column,
				ConfigUtils.camelToTable(this.className)+"_ID").toUpperCase();
	}

	public String getRefProperty() {
		MappingClass c = ConfigUtils.findClass(this.className,getParent().getConfig());
		List os = c.getManyToOne();
		for (Iterator iter = os.iterator(); iter.hasNext();) {
			MappingManyToOne o = (MappingManyToOne) iter.next();
			if(o.getClassName().equals(getParent().getName())){
				return o.getName();
			}
		}
		return StringUtils.uncapitalize(this.className);
	}
	public String getRefId() {
		return StringUtils.uncapitalize(this.className) + "Id";
	}

	public String getClassName() {
		return className;
	}

	public void setClassName(String className) {
		this.className = className;
	}

	public String getColumn() {
		return column;
	}

	public void setColumn(String column) {
		this.column = column;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

//	public String getRefProperty() {
//		return refProperty;
//	}
//
//	public void setRefProperty(String refProperty) {
//		this.refProperty = refProperty;
//	}

	public MappingClass getParent() {
		return parent;
	}

	public void setParent(MappingClass parent) {
		this.parent = parent;
	}

}
